<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker clearable
                                    v-model="queryParams.startTime"
                                    type="date"
                                    value-format="YYYY-MM-DD"
                                    placeholder="请选择开始时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker clearable
                                    v-model="queryParams.endTime"
                                    type="date"
                                    value-format="YYYY-MM-DD"
                                    placeholder="请选择结束时间">
                    </el-date-picker>
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
 <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['exec:file:remove']"
        >删除</el-button>
      </el-col>
     
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="用户名" align="center" prop="username" />
              <el-table-column label="远程路径" align="center" prop="remotePath" />
              <el-table-column label="描述" align="center" prop="description" />
              <el-table-column label="状态" prop="status" align="center" width="120">
               <template #default="scope">
               <dict-tag :options="upload_task_status" :value="scope.row.status" />
            </template>
              </el-table-column>
              <el-table-column label="文件数量" align="center" prop="fileCount" />
              <el-table-column label="主机数量" align="center" prop="hostCount" />
              <!-- <el-table-column label="额外信息" align="center" prop="extraInfo" /> -->
              <el-table-column label="开始时间" align="center" prop="startTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
                <el-table-column label="结束时间" align="center" prop="endTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
                <el-table-column label="创建时间" align="center" prop="startTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="detail(scope.row.id)" v-hasPermi="['exec:file:detail']">详情</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['exec:file:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 弹框详情列表 -->
  <el-dialog
  v-model="open"
  :title="title"
  width="80%"
  @close="cancel"
>
  <el-form :inline="true" :model="detailQueryParams" class="mb8">
    <el-form-item label="地址" prop="hostAddress">
      <el-input v-model="detailQueryParams.hostAddress" placeholder="请输入地址" clearable @keyup.enter="getTaskDetailList" />
    </el-form-item>
    <el-form-item label="状态" prop="status" >
      <el-select v-model="detailQueryParams.status" placeholder="请选择状态" clearable style="min-width: 120px;">
        <el-option
          v-for="item in upload_task_file_status"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="Search" @click="getTaskDetailList">搜索</el-button>
      <el-button icon="Refresh" @click="resetTaskDetailQuery">重置</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="taskDetailList" v-loading="detailLoading" style="width: 100%">
    <el-table-column label="主机名" prop="hostName" />
    <el-table-column label="地址" prop="address" />
    <el-table-column label="文件名" prop="filePath" />
    <el-table-column label="状态" prop="status">
      <template #default="scope">
        <dict-tag :options="upload_task_file_status" :value="scope.row.status" />
      </template>
    </el-table-column>
     <el-table-column label="开始时间" align="center" prop="startTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
                <el-table-column label="结束时间" align="center" prop="endTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
              </el-table-column>
    <el-table-column label="备注" prop="msg" />
  </el-table>
  <pagination
    v-show="detailTotal>0"
    :total="detailTotal"
    v-model:page="detailQueryParams.pageNum"
    v-model:limit="detailQueryParams.pageSize"
    @pagination="getTaskDetailList"
  />
</el-dialog>
 
  </div>
</template>

<script setup name="TaskList">
import { listTask, getTask ,delTask} from "@/api/exec/file"

  const { proxy } = getCurrentInstance()
const { upload_task_status, upload_task_file_status } = proxy.useDict("upload_task_status","upload_task_file_status")
  const taskList = ref([])
  const open = ref(false)
  const loading = ref(true)
  const showSearch = ref(true)
  const total = ref(0)
  const title = ref("")
  const taskDetailList = ref(null)
  const ids = ref([])
  const single = ref(true)
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      startTime: null,
      endTime: null,
    },
    rules: {
    }
  })

  const { queryParams} = toRefs(data)

  /** 查询上传任务列表 */
  function getList() {
    loading.value = true
    listTask(queryParams.value).then(response => {
            taskList.value = response.rows
      total.value = response.total
      loading.value = false
    })
  }

const detailTotal = ref(0)
const detailLoading = ref(false)
const detailQueryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  hostAddress: '',
  status: ''
})
let currentTaskId = null

function detail(id) {
  open.value = true
  title.value = "上传任务详情"
  currentTaskId = id
  resetTaskDetailQuery()
  getTaskDetailList()
}

function getTaskDetailList() {
  detailLoading.value = true
  getTask(currentTaskId, {
    ...detailQueryParams
  }).then(res => {
    taskDetailList.value = res.rows
    detailTotal.value = res.total
    detailLoading.value = false
  }).catch(() => {
    detailLoading.value = false
  })
}

function resetTaskDetailQuery() {
  detailQueryParams.pageNum = 1
  detailQueryParams.pageSize = 10
  detailQueryParams.hostAddress = ''
  detailQueryParams.status = ''
}

  // 取消按钮
  function cancel() {
    open.value = false
    resetTaskDetailQuery()
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef")
    handleQuery()
  }

 // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
  }

   /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value
    proxy.$modal.confirm('是否确认删除上传任务文件编号为"' + _ids + '"的数据项？').then(function() {
      return delTask(_ids)
    }).then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    }).catch(() => {})
  }
  getList()
</script>
